package com.boss.ccs.pam;

import com.boss.Messages;
import com.boss.core.BossConstants;
import com.boss.core.common.Artifact;
import com.boss.core.common.Target;
import com.boss.core.form.BossForm;
import com.boss.core.form.BossFormHelper;
import com.boss.core.util.BossGlobalUtil;
import com.boss.core.util.BossUtil;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Boss PAM Form
 * @author Momin Noor Khan
 */
public class BossPAMForm extends BossForm {
    private String artifacts = "";
    private boolean isArtifactSection = false;
    private boolean isTargetSection;
    private String targetProfile;

    //Getters and Setters
    public boolean isArtifactSection() {
        return isArtifactSection;
    }

    public void setArtifactSection(boolean artifactSection) {
        isArtifactSection = artifactSection;
    }

    public String getArtifacts() {
        return artifacts;
    }

    public void setArtifacts(String artifacts) {
        this.artifacts = artifacts;
    }

    public boolean isTargetSection() {
        return isTargetSection;
    }

    public void setTargetSection(boolean targetSection) {
        isTargetSection = targetSection;
    }

    public String getTargetProfile() {
        return targetProfile;
    }

    public void setTargetProfile(String targetProfile) {
        this.targetProfile = targetProfile;
    }

    public BossPAMForm(StaplerRequest req, JSONObject jsonForm, String project) {
        super(req, jsonForm, project);
        setAvailableJob(BossUtil.findAvailableJob2(project));  //pam is handled differently, so had to override availableJob here
        isArtifactSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_ARTIFACT);
        artifacts = BossFormHelper.getSelectedArtifacts(isArtifactSection, getRequest());
        isTargetSection = BossUtil.contains(getJsonTaskSection(), BossConstants.KEY_TARGET);
        targetProfile = BossFormHelper.getSelectedTargetProfile(isTargetSection, req);
    }

    @Override
    public void prepareUrlParamMap() {
        super.prepareUrlParamMap();
        Map<String, String> map = getUrlMap();
        map.put(BossConstants.URL_PARAM_ARTIFACTS, artifacts);
        map.put(BossConstants.URL_PARAM_TARGET_PROFILE, targetProfile);
        setUrlMap(map);
    }

    @Override
    public boolean isValid() {
        boolean valid = true;
        if (!checkMandatoryFields() |
                !checkArtifacts() |
                !checkTargetProfile()
                ) {
            valid = false;
        } else if (!BossFormHelper.checkAvailableJob(getRequest(), getAvailableJob())) {
            valid = false;
        }
        return valid;
    }

    //Validation methods
    public boolean checkMandatoryFields() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        if (!BossFormHelper.checkTask(req, task)) {
            valid = false;
        }
        return valid;
    }

    /**
     * Performs post-submit validation for artifacts.
     * Validation - At-least one artifact must be selected when the task type is 'deploy'.
     * One of the values from the drop-down must be selected.
     */
    public boolean checkArtifacts() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String artifacts = getArtifacts();
        if (task.equals(BossConstants.TASK_DEPLOY)) {
            if (StringUtils.isBlank(artifacts)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryArtifact(task));
                valid = false;
            }
        }
        return valid;
    }

    /**
     * Performs post-submit validation for target profiles
     * Validation - At least one target container must be selected if the task type is 'deploy'
     * Target container is a combination of environment-server-port instance
     */
    public boolean checkTargetProfile() {
        boolean valid = true;
        StaplerRequest req = getRequest();
        String task = getTask();
        String targetProfile = getTargetProfile();
        if (task.equals(BossConstants.TASK_DEPLOY)) {
            if (StringUtils.isBlank(targetProfile)) {
                BossFormHelper.setValidationErrorMsg(req, Messages.Boss_MandatoryTarget(task));
                valid = false;
            }
        }
        return valid;
    }
}
